import { join } from 'path'
import { createRequire } from 'module'
import os from 'os'
import chalk from 'chalk'
import axios from 'axios'
import { getKey } from './tiny.js'

export const error = console.error

export const log = console.log

export const rcPath = join(os.homedir(), '.tinyrc')

export const cmdTips = chalk.white.underline('tiny key set [apiKey]')

export function progress(progressInfo) {
  return chalk.white.bold(`${progressInfo.current}/${progressInfo.count}`)
}

export function keyTips() {
  log(`当前未设置apiKey, 请使用命令 ${cmdTips} 设置`)
}

export const requireFile = createRequire(import.meta.url)

const uploadDataUrl = 'http://10.110.110.151:4001/tinypng/add'
// const uploadDataUrl = 'http://localhost:3000/tinypng/add'
export function uploadTinyData(tinyInfo) {
  return new Promise((resolve, reject) => {
    axios.post(uploadDataUrl, {
      ...tinyInfo,
      from: '@lishi/tiny-cli'
    }).catch(e => {
      console.log('压缩数据上传失败, 请联系管理员! 错误信息: ', e.response.data)
    }).finally(() => {
      resolve()
    })
  })
}
